package com.olap.starter.controller.admin.luckycola.services;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.olap.starter.controller.admin.luckycola.DBDto;
import com.olap.starter.controller.admin.luckycola.utils.OracleQuery;
import com.olap.starter.controller.admin.luckycola.utils.SqlUtil;
import com.olap.starter.controller.admin.luckycola.utils.DefaultFieldEn;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

import static com.olap.starter.controller.admin.luckycola.utils.DefaultFieldEn.*;

/**
 * Oracle 数据库
 */
@Service
public class OracleAsyncDBService {
    private final OracleQuery oracleQuery = new OracleQuery();
    public void getSql(DBDto dbDto, Map<String, Map<String, Object>> dataSourceMap, List<String> exSqlList) {
        for (Map<String, Object> map : dbDto.getList()) {
            String columnName = map.get(oracleQuery.fieldName()).toString().toUpperCase();
            Object columnNameOld = map.get("COLUMN_NAME_OLD");
            Object delete = map.get("delete");
            Object DEFAULT = map.get("DEFAULT");
            if (SqlUtil.isDefault(DEFAULT)) {
                addDefault(dbDto, columnName, dataSourceMap, exSqlList);
            } else if (columnNameOld == null) {// 新增
                exSqlList.add(SqlUtil.getAddSql(dbDto.getTableName(), columnName.toUpperCase(), map.get(oracleQuery.columnType())));
            } else if (delete != null) {// 删除
                exSqlList.add(SqlUtil.getDropSql(dbDto.getTableName(), columnName));
            } else if (!columnName.equals(columnNameOld.toString())) { // 字段不相同，直接更新
                exSqlList.add(SqlUtil.getRenameSql(dbDto.getTableName(), columnName, columnNameOld));
            }
            // 除了默认字段
            if (!SqlUtil.isDefault(DEFAULT) && ObjectUtils.isEmpty(delete)) {
                if (!ObjectUtils.isEmpty(map.get(oracleQuery.fieldType())) && !CollectionUtils.isEmpty(dataSourceMap.get(columnName))) {
                    if (SqlUtil.dateTypeNotEq(map, dataSourceMap,  columnName)
                            || SqlUtil.lengthNotEq(map, dataSourceMap,  columnName)) {
                        exSqlList.add(SqlUtil.getModifyAllSql(dbDto.getTableName(), columnName, map.get(oracleQuery.columnType()), ""));
                    }
                }
                // 注释
                if (CollectionUtils.isEmpty(dataSourceMap.get(columnName))
                        || (!ObjectUtils.isEmpty(map.get(oracleQuery.fieldComment()))
                        && SqlUtil.commentNotEq(map, dataSourceMap,  columnName))) {
                    exSqlList.add(SqlUtil.getCommentSql(dbDto.getTableName(), columnName.toUpperCase(), map.get(oracleQuery.fieldComment())));
                }
            }
        }
        if (!ObjectUtils.isEmpty(dbDto.getDesc())
                && ObjectUtils.notEqual(dbDto.getDesc(), dataSourceMap.get(ID.getCode()).get(oracleQuery.tableComment()))) {
            exSqlList.add(SqlUtil.getCommentSql(dbDto.getTableName(), "", dbDto.getDesc()));
        }
        if (StringPool.ONE.equals(dbDto.getIsAdd())) {
            exSqlList.add(SqlUtil.getDropDefaultSql(dbDto.getTableName()));
        }
    }
    private void addDefault(DBDto dbDto, String columnName, Map<String, Map<String, Object>> dataSourceMap, List<String> exSqlList) {
        DefaultFieldEn fieldEn = DefaultFieldEn.getByCode(columnName);
        if (ObjectUtils.isEmpty(fieldEn)) {
            return;
        }
        switch (fieldEn){
            case CREATOR:
//                if (CollectionUtils.isEmpty(dataSourceMap.get(columnName))) {
//                    if (dataSourceMap.get("CREATE_USER") == null) {
//                        exSqlList.add(SqlUtil.getAddSql(dbDto.getTableName(), columnName.toUpperCase(), CREATE_BY.getType()));
//                    } else {
//                        // 修改字段名
//                        exSqlList.add(SqlUtil.getRenameSql(dbDto.getTableName(), columnName.toUpperCase(), "CREATE_USER"));
//                    }
//                    if (ObjectUtils.isEmpty(dataSourceMap.get(columnName).get(oracleQuery.fieldComment()))) {
//                        exSqlList.add(SqlUtil.getCommentSql(dbDto.getTableName(), columnName.toUpperCase(), CREATE_BY.getDesc()));
//                    }
//                }
//                break;
            case UPDATER:

                if (CollectionUtils.isEmpty(dataSourceMap.get(columnName))) {
                    if (dataSourceMap.get(fieldEn.getAlias(fieldEn.getCode())) == null) {
                        exSqlList.add(SqlUtil.getAddSql(dbDto.getTableName(), columnName.toUpperCase(), fieldEn.getType()));
                    } else {
                        // 修改字段名
                        exSqlList.add(SqlUtil.getRenameSql(dbDto.getTableName(), columnName.toUpperCase(), fieldEn.getAlias(fieldEn.getCode())));
                    }
                    if (ObjectUtils.isEmpty(dataSourceMap.get(columnName).get(oracleQuery.fieldComment()))) {
                        exSqlList.add(SqlUtil.getCommentSql(dbDto.getTableName(), columnName.toUpperCase(), fieldEn.getDesc()));
                    }
                }
                break;
            case CREATE_TIME:
//                if (CollectionUtils.isEmpty(dataSourceMap.get(columnName))) {
//                    exSqlList.add(SqlUtil.getAddSql(dbDto.getTableName(), columnName.toUpperCase(), CREATE_TIME.getType()));
//                } else {
//                    if (dataSourceMap.get(columnName).get("COLUMN_DEFAULT") == null) {
//                        exSqlList.add(SqlUtil.getModifyAllSql(dbDto.getTableName(), columnName.toUpperCase(), CREATE_TIME.getType(), CREATE_TIME.getValue()));
//                    }
//                }
//                if (ObjectUtils.isEmpty(dataSourceMap.get(columnName).get(oracleQuery.fieldComment()))) {
//                    exSqlList.add(SqlUtil.getCommentSql(dbDto.getTableName(), columnName.toUpperCase(), CREATE_TIME.getDesc()));
//                }
//                break;
            case UPDATE_TIME:
                if (CollectionUtils.isEmpty(dataSourceMap.get(columnName))) {
                    exSqlList.add(SqlUtil.getAddSql(dbDto.getTableName(), columnName.toUpperCase(), fieldEn.getType()));
                } else {
                    if (dataSourceMap.get(columnName).get(oracleQuery.columnDefault()) == null) {
                        exSqlList.add(SqlUtil.getModifyAllSql(dbDto.getTableName(), columnName.toUpperCase(), fieldEn.getType(), fieldEn.getValue()));
                    }
                }
                if (ObjectUtils.isEmpty(dataSourceMap.get(columnName).get(oracleQuery.fieldComment()))) {
                    exSqlList.add(SqlUtil.getCommentSql(dbDto.getTableName(), columnName.toUpperCase(), fieldEn.getDesc()));
                }
                break;
            case REMARK:
            case DELETED:
            case TENANT_ID:
            case ID:
                if (CollectionUtils.isEmpty(dataSourceMap.get(columnName))) {
                    exSqlList.add(SqlUtil.getAddAllSql(dbDto.getTableName(), columnName.toUpperCase(), fieldEn.getType(), fieldEn.getValue(),true));
                }
                if (ObjectUtils.isEmpty(dataSourceMap.get(columnName).get(oracleQuery.fieldComment()))) {
                    exSqlList.add(SqlUtil.getCommentSql(dbDto.getTableName(), columnName.toUpperCase(), fieldEn.getDesc()));
                }
                break;
            default:
        }
    }
}
